package it.polimi.sartori_daniello_picca.meteocal.entity;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;


/**
 *
 * @author Ale
 */
@Entity
@Table
@XmlRootElement
@NamedQueries({
    @NamedQuery (name= "Event.findByAll", query= "SELECT e FROM Event e"),
    @NamedQuery(name = "Event.advancedSearch", query = "SELECT e FROM Event e WHERE e.eventname LIKE :eventname AND e.privacy=false AND e.place LIKE :place AND e.dataevento >= :dataevento "),
    @NamedQuery(name = "Event.findByEventName", query = "SELECT e FROM Event e WHERE e.eventname = :eventname"),
    @NamedQuery (name= "Event.findByEventid", query= "SELECT e FROM Event e WHERE e.eventid  = :eventid"),
    @NamedQuery(name = "Event.findByCreator", query = "SELECT e FROM Event e WHERE e.creator = :user")
})
public class Event implements Serializable {
    
    public static final String findByEventName = "Event.findByEventName";
    public static final String advancedSearch = "Event.advancedSearch";
    public static final String findByEventId = "Event.findByEventId";
    public static final String findByAll = "Event.findByAll";
    public static final String findByCreator = "Event.findByCreator";
    private static final long serialVersionUID = 1L;
    @Basic(optional = false)
    @NotNull
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    @Column(name= "eventid")
    @Id
    private Integer eventid;
  
    @Pattern(regexp="^[a-zA-Z0-9 ]*$",
            message="Invalid Character")
    @Size(max = 300)
    @Column(name = "description")
    private String description;
    
    @Pattern(regexp="^[a-zA-Z0-9 ]*$",
            message="Invalid Character")
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "eventname")
    private String eventname;
    
    @Pattern(regexp="^[a-zA-Z0-9 ]*$",
            message="Invalid Character")
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "place")
    private String place;
     
    @Basic(optional = false)
    @NotNull
    @Column(name = "outdoor")
    private boolean outdoor;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "privacy")
    private boolean privacy;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "dataevento")
    @Temporal(TemporalType.DATE)
    private Date dataevento;
    
    @ManyToOne
    @JoinColumn(name="creator", referencedColumnName="username")
    private Users creator;
            
    public Event() {
    }

    public Event(Integer eventid) {
        this.eventid = eventid;
    }

    public Event(Integer eventid, String description, String eventname, String place, Boolean outdoor, Boolean privacy, Date dataevento, Users creator) {
        this.eventid = eventid;
        this.description = description;
        this.eventname = eventname;
        this.place = place;
        this.outdoor = outdoor;
        this.privacy = privacy;
        this.dataevento = dataevento;
        this.creator = creator;
    }
    
    public Integer getEventId() {
        return eventid;
    }

    public void setEventId(Integer eventid) {
        this.eventid = eventid;
    }
    
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
}
    
    public Date convertToDate(String s) {
        SimpleDateFormat datastr = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = datastr.parse(s);
            return date;
        }
        catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    } 
            
            
    public String getEventName() {
        return eventname;
    }

    public void setEventName(String eventname) {
        this.eventname = eventname;
}
    
    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
}
    
    public Boolean getPrivacy() {
        return privacy;
     }

    public void setPrivacy(Boolean privacy) {
        this.privacy = privacy;
    }
    
    public boolean getOutdoor() {
        return outdoor;
    }

    public void setOutdoor(boolean outdoor) {
        this.outdoor = outdoor;
    }
    
    public Date getDataEvento() {
        return dataevento;
     }

    public void setDataEvento(Date dataevento){
        this.dataevento = dataevento;
    }
    
    public String getDataEventoString(){
        return (dataevento.getDay() +"/"+((dataevento.getMonth())+1)+"/"+((dataevento.getYear())+1900));
    }
   
    public Users getCreator() {
        return creator;
    }
    
    public void setCreator(Users creator) {
        this.creator = creator;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (eventid != null ? eventid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Event)) {
            return false;
        }
        Event other = (Event) object;
        if ((this.eventid == null && other.eventid != null) || (this.eventid != null && !this.eventid.equals(other.eventid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "it.polimi.sartori_daniello_picca.meteocal.entity.Event[ eventid=" + eventid + " ]";
    }

}
